//author:kakake
var Model = /** @class */ (function () {
    function Model() {
    }
    return Model;
}());

//author:kakake
var Common = /** @class */ (function () {
    function Common() {
    }
    return Common;
}());

//author:kakake
var tsFormController = /** @class */ (function () {
    function tsFormController() {
        this.data = {
            dbConn: TSLobsterAPI.Program.GetDbConn(),
            right: TSLobsterAPI.Program.GetLoginData()
        };
        this.formModel = {
            model: {
                "ClassId": "1",
                "Title": "术后复诊诊前问卷",
                "Description": "说明",
                "Introduction": "问卷介绍",
                "Notice": "填写须知",
                "Crowd": "适宜人群",
                "tabooCrowd": "禁忌人群",
                "ageGroup": "适用年龄段",
                "Reference": "参考文献",
                "HeadPic": "图片",
                "ViewId": "28",
                "Id": 0,
                "CreateUser": "",
                "CreateTime": "yyyy-MM-dd HH:mm:ss",
                "UpdateTime": "yyyy-MM-dd HH:mm:ss",
                "ReleaseStatus": 0,
                "DelFlag": 0,
                "WorkId": 1
            },
            tablename: "PSQ_Form",
            key: "Id",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 1
        };
        this.formdataModel = {
            model: {
                "Id": 0,
                "FormId": 0,
                //"FormItem":"",
                "FormValue": "",
                "CreateUser": "",
                "CreateTime": "",
                "UpdateUser": "",
                "UpdateTime": "",
                "WriteTimeLong": "",
                "WriteDevice": "",
                "WriteOS": "",
                "WriteBrower": "",
                "WriteLocation": "",
                "WriteIP": "",
                "DelFlag": 0,
                "WorkId": 1
            },
            tablename: "PSQ_FormData",
            key: "Id",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 1
        };
        this.templateformModel = {
            model: {
                "Id": 0,
                "Title": "术后复诊诊前问卷",
                "Description": "说明",
                "Introduction": "问卷介绍",
                "Notice": "填写须知",
                "Crowd": "适宜人群",
                "tabooCrowd": "禁忌人群",
                "ageGroup": "适用年龄段",
                "Reference": "参考文献",
                "HeadPic": "图片",
                "ViewId": "0",
                "Viewschema": "",
                "FormItem": "",
                "CreateUser": "",
                "CreateTime": "yyyy-MM-dd HH:mm:ss",
                "UpdateTime": "yyyy-MM-dd HH:mm:ss",
                "DelFlag": 0,
                "LabelType1": "",
                "LabelType2": "",
                "LabelType3": ""
            },
            tablename: "PSQ_TemplateForm",
            key: "Id",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 1
        };
    }
    //获取分类psqform.getclasslist
    tsFormController.prototype.getclasslist = function () {
        var strsql = "SELECT Id,ClassName,Description,LabelType1,LabelType2,CreateUser,CreateTime,UpdateTime,DelFlag FROM PSQ_Class WHERE DelFlag=0 and workid=@workid ";
        var dbparams = {
            workid: TSLobsterAPI.Program.GetWorkId()
        };
        return { list: TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams), count: 0 };
    };
    //获取表单
    tsFormController.prototype.getformlist = function () {
        var classid = TSLobsterAPI.Program.RequestQuery("classid", 0);
        var keywords = TSLobsterAPI.Program.RequestQuery("keywords", "");
        var delflag = TSLobsterAPI.Program.RequestQuery("delflag", 0);
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery('page', 0),
            perPage: TSLobsterAPI.Program.RequestQuery('limit', 10),
            totalRecord: 0,
            totalPage: 0
        };
        var dbparams = {
            classid: classid,
            keywords: "%" + keywords + "%",
            delflag: delflag,
            workid: TSLobsterAPI.Program.GetWorkId()
        };
        var strsql = "SELECT Id,ClassId,Title,ViewId,CreateUser,CreateTime,UpdateTime,ReleaseStatus,DelFlag FROM PSQ_Form Where workid=@workid ";
        if (classid != "0") {
            strsql = strsql + " and ClassId=@classid ";
        }
        if (keywords != "") {
            strsql = strsql + " and Title like @keywords ";
        }
        return { list: TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo), count: pageInfo.totalRecord };
    };
    //获取表单对象
    tsFormController.prototype.getformobject = function () {
        var formid = TSLobsterAPI.Program.RequestQuery("formid", 0);
        var strsql = "select * from PSQ_Form where Id=@formid";
        var dbparams = {
            formid: formid
        };
        return { formobj: TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams) };
    };
    //获取表单单条数据
    tsFormController.prototype.getformsingledata = function () {
        var id = TSLobsterAPI.Program.RequestQuery("id", 0);
        var strsql = "SELECT Id	,FormId	,	FormValue	,	CreateTime	,	UpdateTime	,WriteTimeLong	,WriteDevice	,WriteOS	,WriteBrower	,WriteLocation	,WriteIP FROM PSQ_FormData where Id=@id";
        var dbparams = {
            id: id
        };
        var data = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        strsql = "SELECT Title,FormItem FROM PSQ_Form where Id=@formid";
        var dbparams2 = {
            formid: data['FormId']
        };
        var title = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams2);
        var formresult = []; //组装表单结果
        var columnVal = JSON.parse(data.FormValue);
        var columnList = JSON.parse(title.FormItem);
        columnList.forEach(function (item) {
            formresult.push({ "title": item.label, "text": columnVal[item.name] });
        });
        return { "head": title.Title, "formresult": formresult, "otherinfo": data };
    };
    //保存表单数据
    tsFormController.prototype.saveformdata = function () {
        var formobj = JSON.parse(TSLobsterAPI.Program.RequestBody());
        // PublicFunction.TrackLog("saveformdata", 0, formobj);
        var val = {
            Id: 0,
            FormId: formobj.otherinfo.formid,
            //FormItem:"";
            FormValue: JSON.stringify(formobj.formvalue),
            CreateUser: this.data.right ? this.data.right.UserName : "",
            CreateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            UpdateUser: this.data.right ? this.data.right.UserName : "",
            UpdateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            WriteTimeLong: formobj.otherinfo.timelong,
            WriteDevice: formobj.otherinfo.device,
            WriteOS: formobj.otherinfo.os,
            WriteBrower: formobj.otherinfo.browser,
            WriteLocation: formobj.otherinfo.cname,
            WriteIP: formobj.otherinfo.cip,
            DelFlag: 0
        };
        this.formdataModel.objval = val;
        this.formdataModel.WorkId = TSLobsterAPI.Program.GetWorkId();
        // PublicFunction.TrackLog("saveformdata", 0, val);
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.formdataModel);
        return { result: val };
    };
    //保存问卷表单
    tsFormController.prototype.saveform = function () {
        var formobj = JSON.parse(TSLobsterAPI.Program.RequestBody());
        //新增默认值
        if (parseInt(formobj.Id) == 0) {
            formobj.CreateUser = TSLobsterAPI.Program.GetLoginData().UserName;
            formobj.CreateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            formobj.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            formobj.ReleaseStatus = 0;
            formobj.DelFlag = 0;
        }
        else {
            formobj.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
        }
        this.formModel.objval = formobj;
        this.formModel.WorkId = TSLobsterAPI.Program.GetWorkId();
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.formModel);
        //处理Viewschema和FormItem
        return { result: formobj };
    };
    //删除问卷
    tsFormController.prototype.deleteform = function () {
        var formobj = JSON.parse(TSLobsterAPI.Program.RequestBody());
        var id = formobj.Id;
        var flag = formobj.flag;
        //TSLobsterAPI.DbHelper.DeleteModel(this.data.dbConn, this.formModel);
        var strsql = "update psq_form set delflag=@flag where id=@id";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, { id: id, flag: flag });
        return { result: true };
    };
    //发布问卷
    tsFormController.prototype.releaseform = function () {
        var formobj = JSON.parse(TSLobsterAPI.Program.RequestBody());
        //1.获取Viewschema和FormItem
        //2.更新ReleaseStatus状态
        var strsql = null;
        var dbparams = {
            id: formobj.Id
        };
        strsql = "update psq_form set ReleaseStatus=1 where id=@id";
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        return { result: true };
    };
    //设计表单，修改为未发布状态
    tsFormController.prototype.designform = function () {
        var formid = TSLobsterAPI.Program.RequestQuery("formid", 0);
        var strsql = "update psq_form set ReleaseStatus=0 where id=@id";
        var dbparams = {
            id: formid
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        return { result: true };
    };
    //导入表单成功
    tsFormController.prototype.importformsucceed = function () {
        var classid = TSLobsterAPI.Program.RequestQuery("classid", 0);
        var id = TSLobsterAPI.Program.RequestQuery("id", 0);
        //1.查询模板表单
        //2.插入到表单表
        //3.插入到Amis表
        //4.更新ViewId
        //1.
        var strsql = "SELECT * FROM psq_templateform where id=@id";
        var dbparams = {
            id: id
        };
        var templatedata = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (typeof (templatedata.Id) == "undefined") {
            return { result: false };
        }
        //2.
        var form = {
            Id: 0,
            ClassId: classid,
            Title: templatedata.Title,
            Description: templatedata.Description,
            Introduction: templatedata.Introduction,
            Notice: templatedata.Notice,
            Crowd: templatedata.Crowd,
            tabooCrowd: templatedata.tabooCrowd,
            ageGroup: templatedata.ageGroup,
            Reference: templatedata.Reference,
            HeadPic: templatedata.HeadPic,
            ViewId: 0,
            CreateUser: TSLobsterAPI.Program.GetLoginData().UserName,
            CreateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            UpdateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            ReleaseStatus: 0,
            DelFlag: 0
        };
        this.formModel.objval = form;
        this.formModel.WorkId = TSLobsterAPI.Program.GetWorkId();
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.formModel);
        if (form.Id == 0) {
            return { result: false };
        }
        //3.
        // var viewId = PublicFunction.doPost('/amis/v1/AmisData/NewAmisPage', { appid: self.data.appid, label: form.Title, path: 'FormDesign_' + form.Id, schema: templatedata.Viewschema });
        // if (viewId == "") {
        //     return false;
        // }
        //4.
        strsql = "update psq_form set ReleaseStatus=1,Viewschema=@Viewschema,FormItem=@FormItem,ViewId=0 where id=@id";
        var dbparams2 = {
            Viewschema: templatedata.Viewschema,
            FormItem: templatedata.FormItem,
            id: form.Id
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams2);
        return { result: true };
    };
    //存为模板
    tsFormController.prototype.saveastemplateform = function () {
        var formid = TSLobsterAPI.Program.RequestQuery("formid", 0);
        var labeltype1 = TSLobsterAPI.Program.RequestQuery("labeltype1", '');
        var labeltype2 = TSLobsterAPI.Program.RequestQuery("labeltype2", '');
        var labeltype3 = TSLobsterAPI.Program.RequestQuery("labeltype3", '');
        var strsql = "SELECT * FROM PSQ_Form WHERE Id=@formid";
        var dbparams = {
            formid: formid
        };
        var formobj = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        // PublicFunction.TrackLog('saveastemplateform', 0, formobj);
        var templateform = {
            Id: 0,
            Title: formobj.Title,
            Description: formobj.Description,
            Introduction: formobj.Introduction,
            Notice: formobj.Notice,
            Crowd: formobj.Crowd,
            tabooCrowd: formobj.tabooCrowd,
            ageGroup: formobj.ageGroup,
            Reference: formobj.Reference,
            HeadPic: formobj.HeadPic,
            LabelType1: labeltype1,
            LabelType2: labeltype2,
            LabelType3: labeltype3,
            Viewschema: formobj.Viewschema,
            FormItem: formobj.FormItem,
            ViewId: 0,
            CreateUser: TSLobsterAPI.Program.GetLoginData().UserName,
            CreateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            UpdateTime: TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss"),
            DelFlag: 0
        };
        // PublicFunction.TrackLog('saveastemplateform', 0, templateform);
        this.templateformModel.objval = templateform;
        this.templateformModel.WorkId = TSLobsterAPI.Program.GetWorkId();
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.templateformModel);
        return { result: true };
    };
    tsFormController.prototype.getschema = function () {
        var para = TSLobsterAPI.Program.RequestQuery("id", 0);
        var id = TSLobsterAPI.Utils.StringToArray(para, "|")[2];
        var strsql = "SELECT Viewschema FROM PSQ_Form where Id=@id";
        var dbparams = {
            id: id
        };
        var data = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        return { schema: data.Viewschema };
    };
    tsFormController.prototype.saveschema = function () {
        var para = TSLobsterAPI.Program.RequestQuery("id", 0);
        var id = TSLobsterAPI.Utils.StringToArray(para, "|")[2];
        var strsql = null;
        var dbparams = null;
        var schemaobj = JSON.parse(TSLobsterAPI.Program.RequestBody());
        // MirthAPI.TrackLog("schemaobj", schemaobj.body);
        var formitemlist = [];
        schemaobj.body.forEach(function (item) {
            //MirthAPI.TrackLog("2","-----------------------");
            if (item.type == "form") {
                item.body.forEach(function (c) {
                    //MirthAPI.TrackLog("3","-----------------------");
                    formitemlist.push({ "label": c.label, "name": c.name });
                });
            }
        });
        // MirthAPI.TrackLog("formitemlist", formitemlist);
        // PublicFunction.TrackLog("releaseform", 0, formitemlist);
        strsql = "update psq_form set Viewschema=@schemaobj,FormItem=@formitemlist,ReleaseStatus=0 where Id=@id";
        dbparams = {
            schemaobj: JSON.stringify(schemaobj),
            formitemlist: JSON.stringify(formitemlist),
            id: id
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        return { result: true };
    };
    //获取表单
    tsFormController.prototype.getformlist_h5 = function () {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery('page', 0),
            perPage: TSLobsterAPI.Program.RequestQuery('limit', 10),
            totalRecord: 0,
            totalPage: 0
        };
        var dbparams = {
            workid: TSLobsterAPI.Program.GetWorkId()
        };
        var strsql = "SELECT Id,ClassId,Title,ViewId,CreateUser,CreateTime,UpdateTime,HeadPic FROM PSQ_Form Where workid=@workid and ReleaseStatus=1 and DelFlag=0";
        return { data: TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo), count: pageInfo.totalRecord };
    };
    return tsFormController;
}());

//author:kakake
var tsFormService = /** @class */ (function () {
    function tsFormService() {
    }
    return tsFormService;
}());

//author:kakake
var tsFormDao = /** @class */ (function () {
    function tsFormDao() {
        this.dbConn = TSLobsterAPI.Program.GetDbConn();
    }
    return tsFormDao;
}());

